<template>
  <div class="system-ys7CamerasCaptureLog-container">
    <el-card shadow="hover">
      <div class="system-ys7CamerasCaptureLog-search mb15">
        <el-form :model="tableData.param" ref="queryRef" :inline="true" label-width="100px">
          <el-row>
            <el-col :span="8" class="colBlock">
              <el-form-item label="序列号" prop="deviceSerial">
                <el-input v-model="tableData.param.deviceSerial" placeholder="请输入序列号" clearable
                  @keyup.enter.native="ys7CamerasCaptureLogList" />
              </el-form-item>
            </el-col>
            <el-col :span="8" :class="!showAll ? 'colBlock' : 'colNone'">
              <el-form-item>
                <el-button type="primary"
                  @click="ys7CamerasCaptureLogList"><el-icon><ele-Search /></el-icon>搜索</el-button>
                <el-button @click="resetQuery(queryRef)"><el-icon><ele-Refresh /></el-icon>重置</el-button>
                <el-button type="primary" link @click="toggleSearch">
                  {{ word }}
                  <el-icon v-show="showAll"><ele-ArrowUp /></el-icon>
                  <el-icon v-show="!showAll"><ele-ArrowDown /></el-icon>
                </el-button>
              </el-form-item>
            </el-col>
            <el-col :span="8" :class="showAll ? 'colBlock' : 'colNone'">
              <el-form-item label="通道" prop="channel">
                <el-input v-model="tableData.param.channel" placeholder="请输入通道" clearable
                  @keyup.enter.native="ys7CamerasCaptureLogList" />
              </el-form-item>
            </el-col>
            <el-col :span="8" :class="showAll ? 'colBlock' : 'colNone'">
              <el-form-item label="抓拍类型" prop="type">
                <el-select v-model="tableData.param.type" placeholder="请选择抓拍类型" clearable>
                  <el-option v-for="dict in picture_alarm" :key="dict.value" :label="dict.label" :value="dict.value" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8" :class="showAll ? 'colBlock' : 'colNone'">
              <el-form-item label="图片地址" prop="picUrl">
                <el-input v-model="tableData.param.picUrl" placeholder="请输入图片地址" clearable
                  @keyup.enter.native="ys7CamerasCaptureLogList" />
              </el-form-item>
            </el-col>
            <el-col :span="8" :class="showAll ? 'colBlock' : 'colNone'">
              <el-form-item label="抓拍时间" prop="createdAt">
                <el-date-picker clearable style="width: 200px" v-model="tableData.param.createdAt"
                  format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss" type="datetime"
                  placeholder="选择抓拍时间"></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="8" :class="showAll ? 'colBlock' : 'colNone'">
              <el-form-item label="描述" prop="description">
                <el-input v-model="tableData.param.description" placeholder="请输入描述" clearable
                  @keyup.enter.native="ys7CamerasCaptureLogList" />
              </el-form-item>
            </el-col>
            <el-col :span="8" :class="showAll ? 'colBlock' : 'colNone'">
              <el-form-item label="巡航抓拍结果" prop="presetsCap">
                <el-input v-model="tableData.param.presetsCap" placeholder="请输入巡航抓拍结果" clearable
                  @keyup.enter.native="ys7CamerasCaptureLogList" />
              </el-form-item>
            </el-col>
            <el-col :span="8" :class="showAll ? 'colBlock' : 'colNone'">
              <el-form-item>
                <el-button type="primary"
                  @click="ys7CamerasCaptureLogList"><el-icon><ele-Search /></el-icon>搜索</el-button>
                <el-button @click="resetQuery(queryRef)"><el-icon><ele-Refresh /></el-icon>重置</el-button>
                <el-button type="primary" link @click="toggleSearch">
                  {{ word }}
                  <el-icon v-show="showAll"><ele-ArrowUp /></el-icon>
                  <el-icon v-show="!showAll"><ele-ArrowDown /></el-icon>
                </el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <el-row :gutter="10" class="mb8">
          <!-- <el-col :span="1.5">
            <el-button type="primary" @click="handleAdd"
              v-auth="'api/v1/system/ys7CamerasCaptureLog/add'"><el-icon><ele-Plus /></el-icon>新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" :disabled="single" @click="handleUpdate(null)"
              v-auth="'api/v1/system/ys7CamerasCaptureLog/edit'"><el-icon><ele-Edit /></el-icon>修改</el-button>
          </el-col> -->
          <el-col :span="1.5">
            <el-button type="danger" :disabled="multiple" @click="handleDelete(null)"
              v-auth="'api/v1/system/ys7CamerasCaptureLog/delete'"><el-icon><ele-Delete /></el-icon>删除</el-button>
          </el-col>
        </el-row>
      </div>
      <el-table v-loading="loading" :data="tableData.data" height="73vh" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="" align="center" prop="id" min-width="100px" />
        <el-table-column label="序列号" align="center" prop="linkedDeviceSerial.name" min-width="100px" />
        <el-table-column label="通道" align="center" prop="channel" min-width="100px" />
        <el-table-column label="抓拍类型" align="center" prop="type" :formatter="typeFormat" min-width="100px" />
        <el-table-column label="图片地址" align="center" prop="picUrl" min-width="100px">
          <template #default="scope">
            <el-image v-if="scope.row.picUrl" style="width: 100px; height:60px" :src="scope.row.picUrl"
              :zoom-rate="1.2" preview-teleported :max-scale="7" :min-scale="0.2" :preview-src-list="[scope.row.picUrl]" :initial-index="4"
              fit="cover" />
          </template>
        </el-table-column>
        <el-table-column label="抓拍时间" align="center" prop="createdAt" min-width="100px">
          <template #default="scope">
            <span>{{ proxy.parseTime(scope.row.createdAt, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="描述" align="center" prop="description" min-width="100px" />
        <el-table-column label="巡航抓拍结果" align="center" prop="presetsCap" min-width="100px">
          <template #default="scope">
            <el-image  v-if="scope.row.presetsCap" style="width: 100px; height:60px" :src="scope.row.images[0]"
              :zoom-rate="1.2"  preview-teleported  :max-scale="7" :min-scale="0.2" :preview-src-list="scope.row.images" :initial-index="4"
              fit="cover" />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding" min-width="200px" fixed="right">
          <template #default="scope">
            <!-- <el-button type="primary" link @click="handleView(scope.row)"
              v-auth="'api/v1/system/ys7CamerasCaptureLog/view'"><el-icon><ele-View /></el-icon>详情</el-button>
            <el-button type="primary" link @click="handleUpdate(scope.row)"
              v-auth="'api/v1/system/ys7CamerasCaptureLog/edit'"><el-icon><ele-EditPen /></el-icon>修改</el-button> -->
            <el-button type="primary" link @click="handleDelete(scope.row)"
              v-auth="'api/v1/system/ys7CamerasCaptureLog/delete'"><el-icon><ele-DeleteFilled /></el-icon>删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="tableData.total > 0" :total="tableData.total" v-model:page="tableData.param.pageNum"
        v-model:limit="tableData.param.pageSize" @pagination="ys7CamerasCaptureLogList" />
    </el-card>
    <apiV1SystemYs7CamerasCaptureLogAdd ref="addRef" :deviceSerialOptions="deviceSerialOptions"
      @getYs7CamerasItemsDeviceSerial="getYs7CamerasItemsDeviceSerial" :typeOptions="picture_alarm"
      @ys7CamerasCaptureLogList="ys7CamerasCaptureLogList"></apiV1SystemYs7CamerasCaptureLogAdd>
    <apiV1SystemYs7CamerasCaptureLogEdit ref="editRef" :deviceSerialOptions="deviceSerialOptions"
      @getYs7CamerasItemsDeviceSerial="getYs7CamerasItemsDeviceSerial" :typeOptions="picture_alarm"
      @ys7CamerasCaptureLogList="ys7CamerasCaptureLogList"></apiV1SystemYs7CamerasCaptureLogEdit>
    <apiV1SystemYs7CamerasCaptureLogDetail ref="detailRef" :deviceSerialOptions="deviceSerialOptions"
      @getYs7CamerasItemsDeviceSerial="getYs7CamerasItemsDeviceSerial" :typeOptions="picture_alarm"
      @ys7CamerasCaptureLogList="ys7CamerasCaptureLogList"></apiV1SystemYs7CamerasCaptureLogDetail>
      <el-image-viewer v-if="visible" :url-list="urlLists" @close="visible = false" />
  </div>
</template>
<script lang="ts">
import { ItemOptions } from "/@/api/items";
import { toRefs, reactive, onMounted, ref, defineComponent, computed, getCurrentInstance, toRaw } from 'vue';
import { ElMessageBox, ElMessage, FormInstance } from 'element-plus';
import {
  listYs7CamerasCaptureLog,
  delYs7CamerasCaptureLog,
  listYs7Cameras,
} from "/@/api/system/ys7CamerasCaptureLog";
import {
  Ys7CamerasCaptureLogTableColumns,
  Ys7CamerasCaptureLogInfoData,
  Ys7CamerasCaptureLogTableDataState,
} from "/@/views/system/ys7CamerasCaptureLog/list/component/model"
import apiV1SystemYs7CamerasCaptureLogAdd from "/@/views/system/ys7CamerasCaptureLog/list/component/add.vue"
import apiV1SystemYs7CamerasCaptureLogEdit from "/@/views/system/ys7CamerasCaptureLog/list/component/edit.vue"
import apiV1SystemYs7CamerasCaptureLogDetail from "/@/views/system/ys7CamerasCaptureLog/list/component/detail.vue"
export default defineComponent({
  name: "apiV1SystemYs7CamerasCaptureLogList",
  components: {
    apiV1SystemYs7CamerasCaptureLogAdd,
    apiV1SystemYs7CamerasCaptureLogEdit,
    apiV1SystemYs7CamerasCaptureLogDetail
  },
  setup() {
    const { proxy } = <any>getCurrentInstance()
    const loading = ref(false)
    const queryRef = ref()
    const addRef = ref();
    const editRef = ref();
    const detailRef = ref();
    // 是否显示所有搜索选项
    const showAll = ref(false)
    // 非单个禁用
    const single = ref(true)
    // 非多个禁用
    const multiple = ref(true)
    const word = computed(() => {
      if (showAll.value === false) {
        //对文字进行处理
        return "展开搜索";
      } else {
        return "收起搜索";
      }
    })
    // 字典选项数据        
    const {
      picture_alarm,
    } = proxy.useDict(
      'picture_alarm',
    )
    // deviceSerialOptions关联表数据
    const deviceSerialOptions = ref<Array<ItemOptions>>([])
    const state = reactive({
      ids: [],
      visible:false,
      urlLists:[],
      tableData: {
        data: [],
        total: 0,
        loading: false,
        param: {
          pageNum: 1,
          pageSize: 10,
          id: undefined,
          deviceSerial: undefined,
          channel: undefined,
          type: undefined,
          picUrl: undefined,
          createdAt: undefined,
          description: undefined,
          presetsCap: undefined,
          dateRange: []
        },
      },
    });
    // 页面加载时
    onMounted(() => {
      initTableData();
    });
    // 初始化表格数据
    const initTableData = () => {
      ys7CamerasCaptureLogList()
    };
    /** 重置按钮操作 */
    const resetQuery = (formEl: FormInstance | undefined) => {
      if (!formEl) return
      formEl.resetFields()
      ys7CamerasCaptureLogList()
    };
    // 获取列表数据
    const ys7CamerasCaptureLogList = () => {
      loading.value = true
      listYs7CamerasCaptureLog(state.tableData.param).then((res: any) => {
        let list = res.data.list ?? [];
        state.tableData.data = list;
        if(list && list.length){
          list.forEach((item:any) => {
            if(item.presetsCap){
              item.images=JSON.parse(item.presetsCap).map((items:any)=>{
              return items.name
            })
            }
          });
        }
        state.tableData.total = res.data.total;
        loading.value = false
      })
    };
    const toggleSearch = () => {
      showAll.value = !showAll.value;
    }
    //关联ys7_cameras表选项
    const getYs7CamerasItemsDeviceSerial = () => {
      if (deviceSerialOptions.value && deviceSerialOptions.value.length > 0) {
        return
      }
      proxy.getItems(listYs7Cameras, { pageSize: 10000 }).then((res: any) => {
        deviceSerialOptions.value = proxy.setItems(res, 'deviceSerial', 'name')
      })
    }
    // 抓拍类型字典翻译
    const typeFormat = (row: Ys7CamerasCaptureLogTableColumns) => {
      return proxy.selectDictLabel(picture_alarm.value, row.type);
    }
    // 多选框选中数据
    const handleSelectionChange = (selection: Array<Ys7CamerasCaptureLogInfoData>) => {
      state.ids = selection.map(item => item.id)
      single.value = selection.length != 1
      multiple.value = !selection.length
    }
    const handleAdd = () => {
      addRef.value.openDialog()
    }
    const handleUpdate = (row: Ys7CamerasCaptureLogTableColumns) => {
      if (!row) {
        row = state.tableData.data.find((item: Ys7CamerasCaptureLogTableColumns) => {
          return item.id === state.ids[0]
        }) as Ys7CamerasCaptureLogTableColumns
      }
      editRef.value.openDialog(toRaw(row));
    };
    const handleDelete = (row: Ys7CamerasCaptureLogTableColumns) => {
      let msg = '你确定要删除所选数据？';
      let id: number[] = [];
      if (row) {
        msg = `此操作将永久删除数据，是否继续?`
        id = [row.id]
      } else {
        id = state.ids
      }
      if (id.length === 0) {
        ElMessage.error('请选择要删除的数据。');
        return
      }
      ElMessageBox.confirm(msg, '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          delYs7CamerasCaptureLog(id).then(() => {
            ElMessage.success('删除成功');
            ys7CamerasCaptureLogList();
          })
        })
        .catch(() => { });
    }
    const handleView = (row: Ys7CamerasCaptureLogTableColumns) => {
      detailRef.value.openDialog(toRaw(row));
    }
    return {
      proxy,
      addRef,
      editRef,
      detailRef,
      showAll,
      loading,
      single,
      multiple,
      word,
      queryRef,
      resetQuery,
      ys7CamerasCaptureLogList,
      toggleSearch,
      //关联表数据选项
      deviceSerialOptions,
      //关联ys7_cameras表选项获取数据方法
      getYs7CamerasItemsDeviceSerial,
      typeFormat,
      picture_alarm,
      handleSelectionChange,
      handleAdd,
      handleUpdate,
      handleDelete,
      handleView,
      ...toRefs(state),
    }
  }
})
</script>
<style lang="scss" scoped>
.colBlock {
  display: block;
}

.colNone {
  display: none;
}
</style>